<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备" prop="sbh">
        <el-input
          v-model="queryParams.sbh"
          placeholder="请输入设备"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编码" prop="sbCode">
        <el-input
          v-model="queryParams.sbCode"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否在线" prop="onLine">
        <el-input
          v-model="queryParams.onLine"
          placeholder="请输入是否在线"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属组" prop="groupNames">
        <el-input
          v-model="queryParams.groupNames"
          placeholder="请输入所属组"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createrTime">
        <el-date-picker clearable
          v-model="queryParams.createrTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleAdd"
          v-hasPermi="['kong:sbh:add']"
        >加入组</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['kong:sbh:edit']"
        >移除组</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['kong:sbh:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['kong:sbh:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sbhList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编码" align="center" prop="gid" />
      <el-table-column label="设备名称" align="center" prop="sbh" />
      <el-table-column label="设备编码" align="center" prop="sbCode" />
      <el-table-column label="是否在线" align="center" prop="onLine">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sb_online" :value="scope.row.onLine"/>
        </template>
      </el-table-column>
      <el-table-column label="所属组" align="center" prop="groupNames" />
      <el-table-column label="创建时间" align="center" prop="createrTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createrTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['kong:sbh:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kong:sbh:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-select
          v-model="addGroupParam.group"
          :multiple="false"
          filterable
          remote
          reserve-keyword
          placeholder="请输入组名称"
          :remote-method="groups"
          :loading="loadinggroups">
          <el-option
            v-for="item in options"
            :key="item.groupName"
            :label="item.groupName"
            :value="item.groupName">
          </el-option>
        </el-select>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSbh, getSbh, delSbh, addSbh, updateSbh, groupAddUsers, delgroupAddUsers} from "@/api/kong/sbh";
import { listAll } from "@/api/kong/group";
export default {
  name: "Sbh",
  dicts: ['sb_online'],
  data() {
    return {
      // 遮罩层
      loading: true,
      loadinggroups:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      sbhList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dicts:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sbh: null,
        userId: null,
        sbCode: null,
        onLine: null,
        groupNames: null,
        createrTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      options:[{
        groupName: '请选择'
      }],
      addGroupParam:{
        group:'',
        userIds:[]
      }
    };
  },
  created() {
    this.getList();
    this.groups('')
  },
  methods: {
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      listSbh(this.queryParams).then(response => {
        this.sbhList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    groups(keyword){
      this.loadinggroups = true
      const queryParams = {keyword:keyword};
      listAll(queryParams).then(response => {
        this.options = response.data;
      }).finally(() =>{
        this.loadinggroups = false
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gid: [],
        sbh: null,
        userId: null,
        sbCode: null,
        onLine: null,
        groupNames: null,
        createrTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "请选择组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$confirm('此操作将永久移除所选设备所在的组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.addGroupParam.userIds =this.ids
          delgroupAddUsers(this.addGroupParam).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          });
      }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.addGroupParam.userIds =this.ids
      groupAddUsers(this.addGroupParam).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gids = row.gid || this.ids;
      this.$modal.confirm('是否确认删除设备管理编号为"' + gids + '"的数据项？').then(function() {
        return delSbh(gids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kong/sbh/export', {
        ...this.queryParams
      }, `sbh_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
